<template>
	<view class="main codeLogin">
		<u--input placeholder="请输入手机号码" border="none" v-model="phonerNumber" maxlength="11" type="number"
			customStyle="padding:20rpx;background:#f8f8f8" fontSize="16"></u--input>
		<view class="codeInput">
			<u--input placeholder="请输入验证码" border="none" v-model="phoneCode" maxlength="6"
				customStyle="padding:20rpx;background:#f8f8f8;" fontSize="18;"></u--input>
			<u-code :seconds="seconds" ref="uCode" @change="codeChange" change-text="(X)s"></u-code>
			<view @tap="getCode" class="sendCode">{{tips}}</view>
		</view>
		<view style="margin-top:30rpx;"></view>
		<u-button type="primary" text="确定" :loading="loading" @click="login"></u-button>

	</view>
</template>

<script>
	import {
		doSendCode,
		doPhoneLogin
	} from '@/api/api.js';
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {

		data() {
			return {
				loading: false,
				phonerNumber: '',
				phoneCode: '',
				seconds: '120',
				tips: '获取验证码'
			}
		},
		onLoad(options) {

		},

		methods: {
			...mapMutations(['userLogin', 'logout']),
			Vuexlogin(provider) {
				this.userLogin(provider)
				uni.navigateBack({
					delta: 3
				})
			},
			getCode() {
				let _this = this;
				if (!_this.phonerNumber) {
					uni.$u.toast('请输入手机号码');
					return false;
				}
				if (this.$refs.uCode.canGetCode) {
					uni.showLoading({
						title: '正在获取验证码'
					})
					doSendCode({
						userPhone: _this.phonerNumber
					}).then(r => {
						if (r.data.code == 200) {
							uni.$u.toast('验证码已发送');
							this.$refs.uCode.start();
						} else {
							uni.$u.toast(r.data.msg);
						}
					})

				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			codeChange(text) {
				this.tips = text;
			},

			login() {
				let _this = this
				if (!_this.phonerNumber) {
					uni.showToast({
						title: '请输入手机号码',
						icon: 'none'
					})
				} else if (!_this.phoneCode) {
					uni.showToast({
						title: '请输入短信验证码',
						icon: 'none'
					})
				} else {
					doPhoneLogin({
						validateCode: _this.phoneCode,
						userPhone: _this.phonerNumber,
						comeFrom: 'WechatApplet'
					}).then(r => {
						if (r.data.code == 200) {
							uni.$u.toast('登录成功');
							_this.Vuexlogin(r.data.data)
						} else {
							uni.$u.toast(r.data.msg);
						}
					})
				}
			}
		}
	}
</script>
<style scoped lang="scss">
	.main {
		padding: 40rpx;

		.phone {
			margin-top: 100rpx;
		}

		.codeInput {
			margin-top: 30rpx;
			position: relative;

			.sendCode {
				position: absolute;
				right: 20rpx;
				top: 50%;
				transform: translateY(-50%);
				color: $base-color;
				font-size: 28rpx;
			}
		}

	}
</style>